package com.web.exiaodao.remote.controller;

import com.web.exiaodao.remote.service.ApiBannerService;
import com.web.exiaodao.remote.vo.BannerVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.result.XaResult;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author zhanglin
 * @version V1.0
 * @Title: ApiBannerController.java
 * @Package com.web.exiaodao.remote.controller
 * @Description: Banner图信息接口
 * @date 2016-10-22 11:29:21
 */
@Api(value = "Banner", description = "Banner图接口", position = 10)
@Controller
@RequestMapping("/api/banner")
public class ApiBannerController extends BaseController {

    private static final Logger LOGGER = Logger.getLogger(ApiBannerController.class);

    @Autowired
    private ApiBannerService bannerService;

    /**
     * @return
     * @Title: findBannerList
     * @Description: 查询所有Banner图信息
     */
    @ApiOperation(value = "查询Banner图", notes = "查询所有Banner图信息,当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "findBannerList", method = RequestMethod.POST)
    public XaResult<List<BannerVo>> findBannerList(
            @ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
            @ApiParam("页长,字段名:pageSize,默认10") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("类型,字段名:1：首页 2：游学详情 3:机构 4：学乐/艺术品）5：游学banner 6：商城banner, 默认1") @RequestParam(required = false) Integer type,
            @ApiParam("对象id,字段名:objectId;选填,当type不为1的时候有用") @RequestParam(value = "objectId", required = false) Long objectId
    ) throws BusinessException {
        XaResult<List<BannerVo>> xr = new XaResult<List<BannerVo>>();
        Pageable pageable = WebUitl.buildPageRequest(0, Integer.MAX_VALUE, "[{property:'sort',direction:'ASC'},{property:'createTime',direction:'DESC'}]");
        Map<String, Object> filterParams = WebUitl.getParametersStartingWith("{}", "search_");
        try {
            if (XaUtil.isNotEmpty(type)) {
                filterParams = WebUitl.getParametersStartingWith("{'search_EQ_type':'" + type + "'}", "search_");
                if (XaUtil.isNotEmpty(objectId)) {
                    filterParams = WebUitl.getParametersStartingWith(
                            "{'search_EQ_objectId':'"+ type +"'" +
                                    ",'search_EQ_objectId':'"+objectId+"'}", "search_");
                }
            }
            return bannerService.findListEQStatusByFilter(XaConstant.Status.valid, filterParams, pageable);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                xr.error(e.getMessage(), true);
            } else {
                xr.error(XaConstant.Message.error, true);
            }
            LOGGER.error(XaConstant.Message.error, new Throwable(e));
        }
        return xr;
    }

    /**
     * @return
     * @Title: findBannerById
     * @Description: 根据ID查找单条实体
     * //@param modelId
     */
    @ApiOperation(value = "Banner图详情", notes = "查询Banner图详细信息,当返回的code=1时，取出object进行显示,存放为单体对象")
    @ResponseBody
    @RequestMapping(value = "findBannerById", method = RequestMethod.POST)
    public XaResult<BannerVo> findBannerById(
            @ApiParam("编号,字段名:id,必填") @RequestParam(value = "id") Long id
    ) throws BusinessException {
        XaResult<BannerVo> xr = new XaResult<BannerVo>();
        return bannerService.findOne(id);
    }

}

